package de.uni_stuttgart.ipvs.ids.globalstate;

import java.io.Serializable;
import java.util.HashMap;

/**
 * Information of a recorded process state.
 * 
 * @author Team 3/4 Elkhan Dadashov, Ahmed Labib, Dhaval A Shah, Patrick Stortz
 */
@SuppressWarnings("serial")
public class RecordedState implements Serializable {

	/** The ID of the process this state belongs to */
	private int processID;
	/** The balance of the process this state belongs to */
	private double processBalance = 0.0;
	/**
	 * The amount of money in the channels of this process. Key is the sender
	 * ID.
	 */
	private HashMap<Integer, Double> amountsInChannels;

	/**
	 * Create a recorded state of a process
	 * 
	 * @param processID
	 *            the ID of the process this state belongs to
	 * @param processBalance
	 *            the balance of the process this state belongs to
	 */
	public RecordedState(int processID, double processBalance) {
		this.setProcessBalance(processBalance);
		this.amountsInChannels = new HashMap<Integer, Double>();
	}

	/**
	 * Set the balance of the process this state belongs to
	 * 
	 * @param processBalance
	 *            the balance
	 */
	public void setProcessBalance(double processBalance) {
		this.processBalance = processBalance;
	}

	/**
	 * Set the amount of money in a specific channel
	 * 
	 * @param senderID
	 *            the ID of the sender of this channel
	 * @param amount
	 *            the amount to set
	 */
	public void setAmountInSingleChannel(int senderID, double amount) {
		this.amountsInChannels.put(senderID, amount);
	}

	/** @return the sum of the amounts in all channels */
	public double getAmountInAllChannels() {
		if (amountsInChannels != null) {
			double sum = 0.0;
			for (Double amount : amountsInChannels.values()) {
				sum = sum + amount;
			}
			return sum;
		} else {
			return 0.0;
		}
	}

	/** @return the balance of the process this state belongs to */
	public double getProcessBalance() {
		return this.processBalance;
	}

	/** @return the ID of the process this state belongs to */
	public int getProcessID() {
		return this.processID;
	}

	@Override
	public String toString() {
		return "Balance: " + getProcessBalance() + " | Channel amount: "
				+ getAmountInAllChannels() + "$";
	}

}
